//
//  HZHomePictureView.swift
//  HansWeibo
//
//  Created by Hans on 16/8/14.
//  Copyright © 2016年 Hans. All rights reserved.
//

import UIKit

// cell可重用标识
private let HZHomePictureViewCellID = "HZHomePictureViewCellID"
// 间距
private let margin:CGFloat = 5
// cell宽高 80
private let cellWH:CGFloat = 80

class HZHomePictureView: UICollectionView {
    
    var PictureInfo: [HZpicModel]?{
        
        didSet{
            
//            contentLabel.text = "\(PictureInfo?.count ?? 0)"
            
            let size = getPictureViewSize(PictureInfo?.count ?? 0)
            // 更新约束 因为cell是复用，但是PictureView的相对约束对象没有发生改变
            self.snp_updateConstraints { (make) in
                make.size.equalTo(size)
            }
            // 刷新
            reloadData()
        }
    }
    
    
    //  计算配图的Size
    func getPictureViewSize(count: Int)->CGSize{
        // 间距
        let margin:CGFloat = 5
        // cell宽高 80
        let cellWH:CGFloat = 80
        
        // 列数和行数
        let col = count == 4 ? 2 : count > 3 ? 3 : count
        let row = count == 4 ? 2 : (count - 1)/3 + 1
        
        //view  的宽高
        let W = CGFloat(col) * cellWH + CGFloat(col - 1) * margin
        let H = CGFloat(row) * cellWH + CGFloat(row - 1) * margin
        
        
        
        return CGSize(width: W, height: H)
    }

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    private func setupUI(){
        backgroundColor = UIColor.clearColor()
        // 添加控件
//        addSubview(contentLabel)
        // 添加约束
//        contentLabel.snp_makeConstraints { (make) in
//            make.center.equalTo(self)
//        }
        
        // 注册cell
        registerClass(HZHomePictureViewCell.self, forCellWithReuseIdentifier: HZHomePictureViewCellID)
        // 设置代理
        dataSource = self
        
        // 设置layout 
        // collectionViewLayout 中没有itemSize 强转为 UICollectionViewFlowLayout
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        // 设置itemSize
        layout.itemSize = CGSize(width: cellWH, height: cellWH)
        // 垂直和水平间距
        layout.minimumLineSpacing = margin
        layout.minimumInteritemSpacing = margin
    }
    
    
    // MARK 懒加载控件
//    private lazy var contentLabel:UILabel = UILabel(textColor: UIColor.redColor(), textFont: 35)
    
}




extension HZHomePictureView: UICollectionViewDataSource {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return PictureInfo?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(HZHomePictureViewCellID, forIndexPath: indexPath) as! HZHomePictureViewCell
        // 赋值
        cell.picUrl = PictureInfo![indexPath.item]
        return cell
    }
}


// 自定义cell
class HZHomePictureViewCell: UICollectionViewCell {
    
    // 供外界调用
    var picUrl: HZpicModel?{
        didSet{
            bgImageView.HZsetImageWithString(picUrl?.thumbnail_pic, placeholderImageName: "weibo_placeholder")
            
//            print(picUrl?.thumbnail_pic)
            
            // 判断图片的后缀是否是gif
            if let pic = picUrl?.thumbnail_pic where pic.hasSuffix(".gif"){
                // 显示( cell 复用问题 )
                gifImageView.hidden = false
            }else{
                // 隐藏( cell 复用问题 )
                gifImageView.hidden = true
            }
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 设置视图
    func setupUI(){
        backgroundColor = UIColor.clearColor()
        // 添加控件
        contentView.addSubview(bgImageView)
        contentView.addSubview(gifImageView)
        // 添加约束
        bgImageView.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        gifImageView.snp_makeConstraints { (make) in
            make.bottom.trailing.equalTo(contentView)
        }
    }
    
    
    // MARK - 懒加载控件
    // 背景图片
    private lazy var bgImageView: UIImageView = {
       let image = UIImageView(imageName: "weibo_placeholder")
        image.contentMode = .ScaleAspectFill
        image.clipsToBounds = true
        return image
    }()
    
    // gif 图片
    private lazy var gifImageView: UIImageView = UIImageView(imageName: "timeline_image_gif")
}
